<?php

namespace app\api\controller;


use app\common\controller\Api;
use app\common\enum\AliJobTypeEnum;
use app\job\queue\MpsSnapshotNotifyJob;
use app\job\queue\PostAuditJob;
use think\facade\Log;
use think\facade\Queue;

class Callback extends Api
{
    public function aliNotify()
    {
        $data = $this->request->param();

        $message = $data['Message'];
        // 2. 解码 HTML 实体
        $decodedMessage = html_entity_decode($message);

        $messageArray = json_decode($decodedMessage, true);

        $type = strtolower($messageArray['Type']);

        try {
            switch ($type) {
                case AliJobTypeEnum::SNAPSHOT->value:
                    Queue::push(MpsSnapshotNotifyJob::class, $messageArray,  'MpsSnapshotNotifyJob');
                    break;
                default:
                    break;
            }
        } catch (\Throwable $exception) {
            Log::channel('my')->error($exception->getMessage() . PHP_EOL . $exception->getTraceAsString());
            // 如果发生异常，捕获并显示错误信息
            $this->error(__('server error'));
        }

        config('app.env') != "prod" && file_put_contents(root_path() . '/runtime/videoTranscode.txt', date('Y-m-d H:i:s') . ' ' . json_encode($data, 256) . PHP_EOL, FILE_APPEND);
        config('app.env') != "prod" && file_put_contents(root_path() . '/runtime/videoTranscode.txt', date('Y-m-d H:i:s') . ' ' . $decodedMessage . PHP_EOL, FILE_APPEND);
        $this->success();
    }
}